﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace GameBoard
{
    public class BoardsViewModel
    {
        public enum BoardTypes
        {
            All,
            Main,
            Window,
            Target,
            VisualTab
        }

        private static BoardsViewModel _instance;

        public static BoardsViewModel Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new BoardsViewModel();
                }
                return _instance;
            }
        }

        private List<GameBoardVisual> _boards = new List<GameBoardVisual>();

        public List<GameBoardVisual> Boards
        {
            get { return _boards; }
        }

        public void Intialize()
        {
            var temp = VisualsViewModel.Instance;
            temp.SetInitialBackground();
        }

        public GameBoardVisual CreateAndRegisterNewBoard(BoardTypes theType)
        {
            var newB = new GameBoardVisual(theType);
            RegisterBoard(newB);
            return newB;
        }

        public void RemoveBoard(GameBoardVisual targetBoard)
        {
            DeregisterBoard(targetBoard);
        }

        private void DeregisterBoard(GameBoardVisual oldBoard)
        {
            oldBoard.Dispose();
            OnBoardDeregistered(new BoardRegisteredEventArgs(oldBoard));
        }

        public GameBoardVisual MainBoard
        {
            get { return _boards.FirstOrDefault(x => x.BoardType == BoardTypes.Main); }
        }

        public event EventHandler BoardRegistered;

        protected virtual void OnBoardRegistered(BoardRegisteredEventArgs e)
        {
            EventHandler handler = BoardRegistered;
            if (handler != null)
            {
                handler(this, e);
            }
        }

        public event EventHandler BoardDeregistered;

        protected virtual void OnBoardDeregistered(BoardRegisteredEventArgs e)
        {
            EventHandler handler = BoardDeregistered;
            if (handler != null)
            {
                handler(this, e);
            }
        }

        public bool HasAssociatedVisual(Guid characterGuid)
        {
            return VisualsViewModel.Instance.HasAssociatedVisual(characterGuid);
        }

        public MoveablePicture AssociatedVisual(Guid characterGuid)
        {
            if (HasAssociatedVisual(characterGuid))
                return VisualsViewModel.Instance.MatchingVisual(characterGuid);
            return null;
        }


        public void ClearAllBoards()
        {
            foreach (var cur in Boards)
            {
                DeregisterBoard(cur);
            }
            Boards.Clear();
        }


        public void ImportViewModelFromSave(VisualsViewModel vm)
        {
            VisualsViewModel.Instance.ClearViewModel();
            VisualsViewModel.Instance.OpenBoardInfo(vm.CurrentBoardInfo);
            VisualsViewModel.Instance.OpenVisuals(vm.CharactersToMoveablePicture);
            VisualsViewModel.Instance.OpenTerrain(vm.TerrainViewModel);
        }

        public void RegisterBoard(GameBoardVisual newB)
        {
            foreach (GameBoardVisual curboard in _boards)
            {
            }
            VisualsViewModel.Instance.RegisterGameBoardVisual(newB);
            _boards.Add(newB);
            OnBoardRegistered(new BoardRegisteredEventArgs(newB));
        }


        public void ZoomTo(List<Guid> visuals, BoardTypes theType)
        {
            VisualsViewModel.Instance.ZoomTo(visuals, theType);
        }

        public void ToggleInvisible(List<Guid> activeCharacters, BoardTypes theType)
        {
            VisualsViewModel.Instance.ToggleCharacterInvisible(activeCharacters, theType);
        }

    }

    public enum VisiblityType
    {
        Visible, Translucent, Invisible
    }
}